package com.project.ereader;

import java.util.List;

import com.project.services.IServiceDownload;
import com.project.services.ServiceDownload;
import com.project.services.ServiceMediaPlayer;
import com.project.utility.DownloadTask;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class DownloadManageActivity extends Activity {

	private static IServiceDownload serviceDownload;
	private LinearLayout linearLayout;
	private Context context;
	private List<DownloadTask> tasks;
	private boolean destory = false;
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			//progressBar.setProgress(msg.what);
			DownloadTask task = (DownloadTask)msg.obj;
			if(task.getProgressbar()==null)
				return;
			if (task.isBegin()) {
				TextView textView = new TextView(context);
				textView.setText(task.getDescription());
				task.getProgressbar().setMax(task.getContentLength());
				linearLayout.addView(textView);
				linearLayout.addView(task.getProgressbar());
				task.setBegin(false);
			}
			task.getProgressbar().setProgress(task.getCurrent());
		};
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_download_manage);
		initializeServices();
		linearLayout = (LinearLayout)findViewById(R.id.linearLayout);
		context = this;
	}
	
	@Override
	protected void onDestroy(){
		destory=true;
		if(tasks==null){
			super.onDestroy();
			return;
		}	
		for (DownloadTask taskItem : tasks) {
			taskItem.setProgressbar(null);
			taskItem.setBegin(true);
		}
		super.onDestroy();
	}


	private ServiceConnection downloadServiceConnection = new ServiceConnection() {

		@Override
		public void onServiceDisconnected(ComponentName name) {

		}

		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			serviceDownload = (IServiceDownload) service;
			loadProgress();
		}
	};

	private void loadProgress() {	
		
		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				while (true) {
					if(destory)
						return;
					tasks = serviceDownload.getProcess();
					boolean hasFinishedTask = true;
					for (DownloadTask item : tasks) {
						if (!item.isFinished()) {
							hasFinishedTask = item.isFinished();
							break;
						}
					}
					
					if(hasFinishedTask||tasks.size()==0){
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
						continue;
					}

					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					for (DownloadTask taskItem : tasks) {
						if(taskItem.isBegin()&&!destory){
							ProgressBar barItem = new ProgressBar(context,null,android.R.attr.progressBarStyleHorizontal);
							taskItem.setProgressbar(barItem);
						}
						handler.sendMessage(handler.obtainMessage(taskItem.getCurrent(),taskItem));
					}
					
				}

			}
		});

		thread.start();

	}

	private void initializeServices() {
		if (serviceDownload != null){
			loadProgress();
			return;
		}
		Intent serviceDownloadIntent = new Intent(this, ServiceDownload.class);
		bindService(serviceDownloadIntent, downloadServiceConnection,
				Context.BIND_AUTO_CREATE);
	}

}
